package com.example.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class PuEmployee {

	public static void main(String[] args) {
		try {
			// 1 指定WebService服务的请求地址：
			String wsUrl = "http://47.94.10.241:5888/services/getStaffPage";

			// 2 创建URL：
			URL url = new URL(wsUrl);
			// 3 建立连接，并将连接强转为Http连接
			URLConnection conn = url.openConnection();
			HttpURLConnection con = (HttpURLConnection) conn;

			// 4，设置请求方式和请求头：
			con.setDoInput(true); // 是否有入参
			con.setDoOutput(true); // 是否有出参
			con.setRequestMethod("GET"); // 设置请求方式
			con.setRequestProperty("content-type", "text/xml;charset=UTF-8");

	 

	  String requestBody = " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:get=\"http://www.apusic.com/esb/getStaffPage\">\r\n"
	  		+ "   <soapenv:Header/>\r\n"
	  		+ "   <soapenv:Body>\r\n"
	  		+ "      <get:getStaffPage>\r\n"
	  		+ "         <get:pageNo>20</get:pageNo>\r\n"
	  		+ "         <get:pageSize>100</get:pageSize>\r\n"
	  		+ "         <get:unit_id></get:unit_id>\r\n"
	  		+ "         <get:state>4</get:state>\r\n"
	  		+ "         <get:status>1</get:status>\r\n"
	  		+ "      </get:getStaffPage>\r\n"
	  		+ "   </soapenv:Body>\r\n"
	  		+ "</soapenv:Envelope>";

			// 6，通过流的方式将请求体发送出去：
			OutputStream out = con.getOutputStream();
			out.write(requestBody.getBytes());
			out.close();
			// 7，服务端返回正常：
			int code = con.getResponseCode();
			if (code == 200) {// 服务端返回正常
				InputStream is = con.getInputStream();
				byte[] b = new byte[1024];
				StringBuffer sb = new StringBuffer();
				int len = 0;
				while ((len = is.read(b)) != -1) {
					String str = new String(b, 0, len, "UTF-8");
					sb.append(str);
				}
				System.out.println(sb.toString());
				is.close();
			}
			con.disconnect();
		} catch (Exception e) {
       // TODO: handle exception
			e.printStackTrace();
		}
	}
}
